package com.sqfw.project.wpReport.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageInfo;
import com.sqfw.common.constant.HttpStatus;
import com.sqfw.common.utils.StringUtils;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.project.system.domain.SysDept;
import com.sqfw.project.system.domain.SysDictData;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.mapper.SysDeptMapper;
import com.sqfw.project.system.mapper.SysUserMapper;
import com.sqfw.project.system.service.ISysDictDataService;
import com.sqfw.project.wp.domain.SysCode;
import com.sqfw.project.wp.domain.WpHandleLog;
import com.sqfw.project.wp.domain.WpVisitInfo;
import com.sqfw.project.wp.mapper.SysCodeMapper;
import com.sqfw.project.wp.mapper.WpHandleLogMapper;
import com.sqfw.project.wp.mapper.WpInfoMapper;
import com.sqfw.project.wp.mapper.WpVisitInfoMapper;
import com.sqfw.project.wp.service.ISysCodeService;
import com.sqfw.project.wp.utils.ListUtils;
import com.sqfw.project.wpReport.domain.WpInfoDetail;
import com.sqfw.project.wpReport.mapper.WpInfoDetailMapper;
import com.sqfw.project.wpReport.service.IWpInfoDetailService;
import com.sqfw.project.zj.domain.ZjRecord;
import com.sqfw.project.zj.mapper.ZjRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Slf4j
public class WpInfoDetailServiceImpl implements IWpInfoDetailService {

    @Autowired
    private WpInfoDetailMapper wpInfoDetailMapper;

    @Autowired
    private SysCodeMapper sysCodeMapper;

    @Autowired
    private WpVisitInfoMapper wpVisitInfoMapper;

    @Autowired
    private WpHandleLogMapper wpHandleLogMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ZjRecordMapper zjRecordMapper;

    @Autowired
    private ISysCodeService sysCodeService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    @Qualifier(value = "asyncServiceExecutor")
    private Executor executor;


    @Override
    public Map<String, Object> getWpInfoDetailList(WpInfoDetail wpInfoDetail) {
        //内容分类查询条件
        List<List<String>> classAllFind = wpInfoDetail.getClassAllFind();

        //内容分类查询
        if (StringUtils.isEmpty(classAllFind)) {
            wpInfoDetail.setClassAll(null);
        } else {
            List<String> classAllCon = new ArrayList<>();
            for (List<String> strings : classAllFind) {
                classAllCon.add(strings.get(strings.size() - 1));
            }
            wpInfoDetail.setClassAll(classAllCon);
        }
        if ("是".equals(wpInfoDetail.getEndState())) {
            wpInfoDetail.setEndState("流转");
        }
        if ("否".equals(wpInfoDetail.getEndState())) {
            wpInfoDetail.setEndState(null);
            wpInfoDetail.setUnEndState("流转");
        }
        if ("1".equals(wpInfoDetail.getBackState())){
            wpInfoDetail.setBackStatusType("不等于");
        }else if ("0".equals(wpInfoDetail.getBackState())){
            wpInfoDetail.setBackStatusType("等于");
        }
        List<WpInfoDetail> wpids = wpInfoDetailMapper.getWpInfoDetailWpids(wpInfoDetail);

        Map<String, Object> map = new HashMap<>();
        List<WpInfoDetail> wpInfoDetails = new ArrayList<>();
        map.put("wpids", wpids);
        if (wpids.size() == 0) {
            map.put("wpInfoDetails", new ArrayList<WpInfoDetail>());
            return map;
        }
        for (WpInfoDetail detailCon : wpids) {
            String wpid = detailCon.getWpid();
            WpInfoDetail detail = wpInfoDetailMapper.getWpInfoDetailByWpid(wpid);
            if (StringUtils.isNotEmpty(detail.getEvaluateSatis())&&"1".equals(detail.getEvaluateSatis())){
                detail.setEvaluateSatis("是");
            }else {
//                detail.setEvaluateSatis("否");
                detail.setEvaluateSatis("");
            }
            if ("流转".equals(detail.getEndState())) {
                detail.setEndState("是");
            } else {
                detail.setEndState("否");
            }
            if ("1".equals(detail.getRecoverState())) {
                detail.setRecoverState("是");
            } else {
                detail.setRecoverState("否");
            }

            if ("1".equals(detail.getBackState())&&(!"18359".equals(detail.getClass2()))) {
                detail.setBackState("是");
            } else {
                detail.setBackState("否");
            }
            if (StringUtils.isNotEmpty(wpInfoDetail.getSatisfaction())) {
                detail.setSatisfaction(wpInfoDetail.getSatisfaction());
            } else {
                ZjRecord zjRecord = zjRecordMapper.selectZjRecordByWpid(wpid);
                if (zjRecord != null) {
                    detail.setSatisfaction(zjRecord.getSatisfaction());
                }
            }
            if (StringUtils.isNotEmpty(detail.getCode())) {
                SysUser sysUser = sysUserMapper.selectUserByCode(detail.getCode());
                if (sysUser != null) {
                    if (StringUtils.isEmpty(sysUser.getNickName())){
                        detail.setNickName(sysUser.getCode());
                    }else {
                        detail.setNickName(sysUser.getNickName());
                    }

                }
            }
            String class1 = detail.getClass1();
            String class2 = detail.getClass2();


            String class3 = detail.getClass3();
            String class4 = detail.getClass4();

            if (StringUtils.isEmpty(detail.getFiveMajorModules())) {
                if (StringUtils.isNotEmpty(class2)){
                    SysCode sysCode1 = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(class2));
                    if (sysCode1 != null) {
                        if (StringUtils.isNotEmpty(sysCode1.getFiveMajorModules())) {
                            detailCon.setFiveMajorModules(sysCode1.getFiveMajorModules());
                        }
                    }
                }

                if (StringUtils.isNotEmpty(class3)){
                    SysCode sysCode2 = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(class3));
                    if (sysCode2 != null) {
                        if (StringUtils.isNotEmpty(sysCode2.getFiveMajorModules())) {
                            detailCon.setFiveMajorModules(sysCode2.getFiveMajorModules());
                        }
                    }
                }

            }

            String district = detail.getDistrict();
            //区域
            if (StringUtils.isNotEmpty(district)) {
                SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(district));
                if (sysCode != null) {
                    detail.setDistrictName(sysCode.getNodename());
                }
            }


            if (StringUtils.isNotEmpty(class1)) {
                SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(class1));
                if (sysCode != null) {
                    detail.setClass1Name(sysCode.getNodename());

                }
            }
            if (StringUtils.isNotEmpty(class2)) {
                SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(class2));
                if (sysCode != null) {
                    detail.setClass2Name(sysCode.getNodename());

                }
            }
            if (StringUtils.isNotEmpty(class3)) {
                SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(class3));
                if (sysCode != null) {
                    detail.setClass3Name(sysCode.getNodename());
                }
            }
            if (StringUtils.isNotEmpty(class4)) {
                SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(class4));
                if (sysCode != null) {
                    detail.setClass4Name(sysCode.getNodename());
                }
            }
            //工单是否回访过
            if ("1".equals(detail.getVisitState())) {
                WpVisitInfo wpVisitInfo = wpVisitInfoMapper.selectWpVisitInfoByWpid(detail.getWpid());
                if (wpVisitInfo != null) {
                    if (StringUtils.isNotEmpty(wpInfoDetail.getIssatisfied())) {
                        detail.setIssatisfied(wpInfoDetail.getIssatisfied());
                    } else {
                        detail.setIssatisfied(wpVisitInfo.getIssatisfied());
                    }
                    SysUser sysUser = sysUserMapper.selectUserByCode(wpVisitInfo.getVisitWorkNo());
                    if (sysUser != null) {
                        detail.setVisitCode(sysUser.getNickName());
                    }
                    detail.setVisitResult(wpVisitInfo.getVisitResult());
                    detail.setIssatisfiedReason(wpVisitInfo.getIssatisfiedReason());
                    detail.setVisitTime(wpVisitInfo.getVisitTime());
                }

            }
            //承办单位
            if (StringUtils.isNotEmpty(detail.getDeptLevel2vice1())){
                SysDept sysDept = sysDeptMapper.selectDeptById(Long.valueOf(detail.getDeptLevel2vice1()));
                if (sysDept!=null){
                    detail.setAssistUnit1Name(sysDept.getDeptName());
                }

            }

            if (StringUtils.isNotEmpty(detail.getDeptLevel2vice2())){
                SysDept sysDept = sysDeptMapper.selectDeptById(Long.valueOf(detail.getDeptLevel2vice2()));
                if (sysDept!=null){
                    detail.setAssistUnit2Name(sysDept.getDeptName());
                }
            }


            if (StringUtils.isNotEmpty(detail.getDeptLevel2())) {

                String deptLevel2 = detail.getDeptLevel2();
                SysDept sysDept = sysDeptMapper.selectDeptById(Long.valueOf(deptLevel2));
                if (sysDept != null) {
                    detail.setMainUnitName(sysDept.getDeptName());
                }

                WpHandleLog log = new WpHandleLog();
                log.setNextState("承办单位处理");
                log.setWpid(detail.getWpid());
                wpHandleLogMapper.selectWpHandleLogList(log).stream()
                        .max(Comparator.comparing(WpHandleLog::getUpdateTime))
                        .ifPresent(handleLog -> {
                            //流转时间
                            detail.setEndStateTime(handleLog.getUpdateTime());
                            //流转截止时间
                            log.setNextState("工单回访");
                            List<WpHandleLog> wpHandleLogs = wpHandleLogMapper.selectWpHandleLogList(log);
                            if (wpHandleLogs.size() > 0) {
                                WpHandleLog wpHandleLog = wpHandleLogs.get(0);
                                if (wpHandleLog != null) {
                                    if (wpHandleLog.getUpdateTime().compareTo(handleLog.getUpdateTime()) > 0) {
                                        detail.setCompletionDate(wpHandleLog.getUpdateTime());
                                    }
                                }
                            }
                        });
                //承办单位处理内容
                log.setLastState("承办单位处理");
                log.setNextState(null);
                wpHandleLogMapper.selectWpHandleLogList(log).stream()
                        .max(Comparator.comparing(WpHandleLog::getUpdateTime))
                        .ifPresent(handleLog -> {
                            //承办单位处理内容
                            detail.setMessage(handleLog.getMessage());
                            //是否解决
                            detail.setSolved(handleLog.getSolved());
                        });
            }

            wpInfoDetails.add(detail);

        }
        map.put("wpInfoDetails", wpInfoDetails);

        return map;
    }

    @Override
    public List<WpInfoDetail> exportWpInfoDetailList(WpInfoDetail wpInfoDetail) {

        //内容分类查询条件
        List<List<String>> classAllFind = wpInfoDetail.getClassAllFind();

        //内容分类查询
        if (StringUtils.isEmpty(classAllFind)) {
            wpInfoDetail.setClassAll(null);
        } else {
            List<String> classAllCon = new ArrayList<>();
            for (List<String> strings : classAllFind) {
                classAllCon.add(strings.get(strings.size() - 1));
            }
            wpInfoDetail.setClassAll(classAllCon);
        }
        if ("是".equals(wpInfoDetail.getEndState())) {
            wpInfoDetail.setEndState("流转");
        }
        if ("否".equals(wpInfoDetail.getEndState())) {
            wpInfoDetail.setEndState(null);
            wpInfoDetail.setUnEndState("流转");
        }

        //class1，class2，class3，class4
        List<SysCode> class1Codes = sysCodeService.selectSysCodeClass1List("class1", null);

        List<Long> class2pids = class1Codes.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        List<SysCode> class2Codes = sysCodeService.selectSysCodeClass1List("class2", class2pids);

        List<Long> class3pids = class2Codes.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        List<SysCode> class3Codes = sysCodeService.selectSysCodeClass1List("class3", class3pids);


        List<Long> class4pids = class3Codes.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        List<SysCode> class4Codes = sysCodeService.selectSysCodeClass1List("class4", class4pids);

        SysCode sysCodeCon = new SysCode();

        sysCodeCon.setNodetype("DISTRICT");

        //获取所有街道
        List<SysCode> districts = sysCodeMapper.selectSysCodeListByNodetypes(sysCodeCon);
        //获取allusers
        List<SysUser> sysUsers = sysUserMapper.selectUserList(new SysUser());


        //所有部门
        List<SysDept> depts = sysDeptMapper.selectDeptList(new SysDept());

        //工单标签
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("tag_name");
        List<SysDictData> sysDictDatas = sysDictDataService.selectDictDataList(sysDictData);


        List<WpInfoDetail> wpInfoDetails = wpInfoDetailMapper.getWpInfoDetailWpids(wpInfoDetail);
        List<String> wpids222 = wpInfoDetails.stream().map(WpInfoDetail::getWpid).collect(Collectors.toList());
        List<List<String>> split = ListUtils.split(wpids222, 1000);

        List<WpInfoDetail> wpInfoDetailListAll = new ArrayList<>();
        if (split == null){
            return wpInfoDetailListAll;
        }
        //创建异步阻塞任务集合
        List<CompletableFuture<List<WpInfoDetail>>> futureList = new ArrayList<>();
        for (List<String> wpids : split) {
            futureList.add(CompletableFuture.supplyAsync(() -> getWpInfoDetailListFutule(wpids,sysDictDatas,sysUsers,districts,class1Codes,class2Codes,class3Codes,class4Codes,depts,wpInfoDetail), executor));


        }

        //等待所有任务执行完成
        CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0]));
        List<List<WpInfoDetail>> collect = futureList.stream().map(CompletableFuture::join).collect(Collectors.toList());
        for (List<WpInfoDetail> details : collect) {
            wpInfoDetailListAll.addAll(details);
        }
        return wpInfoDetailListAll;
    }


    private List<WpInfoDetail> getWpInfoDetailListFutule(List<String> wpids,List<SysDictData> sysDictDatas,List<SysUser> sysUsers,
        List<SysCode> districts,List<SysCode> class1Codes,List<SysCode> class2Codes,List<SysCode> class3Codes,List<SysCode> class4Codes,
        List<SysDept> depts,WpInfoDetail wpInfoDetail
    ){


        //Detail获取集合
        List<WpInfoDetail> wpInfoDetailList = wpInfoDetailMapper.getWpInfoDetailByWpids(wpids);


        //回访集合
        List<WpVisitInfo> wpVisitInfos = wpVisitInfoMapper.selectWpVisitInfoByWpidsAndVisitTimeASC(wpids);


        //获取具有承办单位的工单的工单轨迹记录
        List<String> wpidsByDept = wpInfoDetailList.stream()
                .filter(infodetail -> StringUtils.isNotEmpty(infodetail.getDeptLevel2()))
                .map(WpInfoDetail::getWpid)
                .collect(Collectors.toList());

        //获取承办单位处理轨迹记录
        WpHandleLog wpHandleLog = new WpHandleLog();
        wpHandleLog.setNextState("承办单位处理");
        wpHandleLog.setWpids(wpidsByDept);
        //流转到承办单位时间
        List<WpHandleLog> wpHandleLogs = wpHandleLogMapper.selectWpidListByWpidsAndState(wpHandleLog);


        WpHandleLog logHandle = new WpHandleLog();
        logHandle.setNextState("工单回访");
        logHandle.setWpids(wpidsByDept);
        //承办单位办结时间
        List<WpHandleLog> wpHandleLogsVisit = wpHandleLogMapper.selectWpidListByWpidsAndState(logHandle);


        WpHandleLog logCon = new WpHandleLog();
        logCon.setLastState("承办单位处理");
        logCon.setWpids(wpidsByDept);
        //承办单位处理内容
        List<WpHandleLog> wpHandleLogsDept = wpHandleLogMapper.selectWpidListByWpidsAndState(logCon);

        //所有工单对应的录音
        List<ZjRecord> zjRecords = zjRecordMapper.selectZjRecordByWpids(wpids);

        for (WpInfoDetail detail : wpInfoDetailList) {

            if (StringUtils.isNotEmpty(detail.getDictData())){
                String[] dictCodes = detail.getDictData().split(",");
                List<String> strings = Arrays.asList(dictCodes);

                String collect = sysDictDatas.stream()
                        .filter(sysData -> strings.contains(sysData.getDictCode().toString()))
                        .map(SysDictData::getDictValue).collect(Collectors.joining("/"));
                detail.setDictData(collect);
            }

            if (StringUtils.isNotEmpty(detail.getEvaluateSatis())&&"1".equals(detail.getEvaluateSatis())){
                detail.setEvaluateSatis("是");
            }else {
                detail.setEvaluateSatis("否");
            }
            if ("流转".equals(detail.getEndState())) {
                detail.setEndState("是");
            } else {
                detail.setEndState("否");
            }
            if ("1".equals(detail.getRecoverState())) {
                detail.setRecoverState("是");
            } else {
                detail.setRecoverState("否");
            }
            //话后满意度
            if (StringUtils.isNotEmpty(detail.getSendSatis())) {
                if (detail.getSendSatis().equals("1")) {
                    detail.setSendSatis("满意");
                } else if (detail.getSendSatis().equals("2")) {
                    detail.setSendSatis("一般");
                } else if (detail.getSendSatis().equals("3")) {
                    detail.setSendSatis("不满意");
                } else if (detail.getSendSatis().equals("0")) {
                    detail.setSendSatis(null);
                }
            }

            if ("1".equals(detail.getBackState())&&(!"18359".equals(detail.getClass2()))) {
                detail.setBackState("是");
            } else {
                detail.setBackState("否");
            }
            if (StringUtils.isNotEmpty(detail.getCode())) {
                if (sysUsers.size() > 0) {
                    sysUsers.stream()
                            .filter(sysUser -> detail.getCode().equals(sysUser.getCode()))
                            .findAny().ifPresent(user -> {
                        if (StringUtils.isEmpty(user.getNickName())) {
                            detail.setNickName(user.getCode());
                        } else {
                            detail.setNickName(user.getNickName());
                        }

                    });
                }

            }
            if (StringUtils.isNotEmpty(detail.getDistrict())) {
                if (districts.size() > 0) {
                    districts.stream()
                            .filter(sysCode -> sysCode.getAutoId().toString().equals(detail.getDistrict()))
                            .findAny().ifPresent(code -> detail.setDistrictName(code.getNodename()));
                }

            }

            if (StringUtils.isNotEmpty(detail.getClass1())) {
                class1Codes.stream().filter(sysCode -> sysCode.getAutoId().toString().equals(detail.getClass1()))
                        .findAny().ifPresent(code -> detail.setClass1Name(code.getNodename()));
            }

            if (StringUtils.isNotEmpty(detail.getClass2())) {
                class2Codes.stream().filter(sysCode -> sysCode.getAutoId().toString().equals(detail.getClass2()))
                        .findAny().ifPresent(code -> {
                    detail.setClass2Name(code.getNodename());
                    if (StringUtils.isNotEmpty(code.getFiveMajorModules()) && StringUtils.isEmpty(detail.getFiveMajorModules())) {
                        detail.setFiveMajorModules(code.getFiveMajorModules());
                    }

                });
            }

            if (StringUtils.isNotEmpty(detail.getClass3())) {
                class3Codes.stream().filter(sysCode -> sysCode.getAutoId().toString().equals(detail.getClass3()))
                        .findAny().ifPresent(code -> {
                    detail.setClass3Name(code.getNodename());
                    if (StringUtils.isNotEmpty(code.getFiveMajorModules()) && StringUtils.isEmpty(detail.getFiveMajorModules())) {
                        detail.setFiveMajorModules(code.getFiveMajorModules());
                    }
                });
            }

            if (StringUtils.isNotEmpty(detail.getClass4())) {
                class4Codes.stream().filter(sysCode -> sysCode.getAutoId().toString().equals(detail.getClass4()))
                        .findAny().ifPresent(code -> detail.setClass4Name(code.getNodename()));
            }


            //工单是否回访过
            if ("1".equals(detail.getVisitState())) {

                    /*if (StringUtils.isNotEmpty(wpInfoDetail.getIssatisfied())) {
                        detail.setIssatisfied(wpInfoDetail.getIssatisfied());
                    } else {
                        List<WpVisitInfo> wpVisitInfoList = wpVisitInfos.stream()
                                .filter(wpVisitInfo -> detail.getWpid().equals(wpVisitInfo.getWpid()))
                                .collect(Collectors.toList());
                        if (wpVisitInfoList.size() > 0) {
                            WpVisitInfo visitInfo = wpVisitInfoList.get(wpVisitInfoList.size() - 1);
                            detail.setIssatisfied(visitInfo.getIssatisfied());
                            detail.setVisitResult(visitInfo.getVisitResult());
                            detail.setIssatisfiedReason(visitInfo.getIssatisfiedReason());
                        }

                    }*/
                List<WpVisitInfo> wpVisitInfoList = wpVisitInfos.stream()
                        .filter(wpVisitInfo -> detail.getWpid().equals(wpVisitInfo.getWpid()))
                        .collect(Collectors.toList());
                if (wpVisitInfoList.size() > 0) {
                    WpVisitInfo visitInfo = wpVisitInfoList.get(wpVisitInfoList.size() - 1);
                    detail.setIssatisfied(visitInfo.getIssatisfied());
                    detail.setVisitResult(visitInfo.getVisitResult());
                    detail.setVisitTime(visitInfo.getVisitTime());
                    detail.setIssatisfiedReason(visitInfo.getIssatisfiedReason());
                    if (sysUsers.size() > 0) {
                        sysUsers.stream()
                                .filter(sysUser -> visitInfo.getVisitWorkNo().equals(sysUser.getCode()))
                                .findAny().ifPresent(user -> detail.setVisitCode(user.getNickName()));
                    }
                }
            }


            if (StringUtils.isNotEmpty(wpInfoDetail.getSatisfaction())) {
                detail.setSatisfaction(wpInfoDetail.getSatisfaction());
            } else {

                if (zjRecords.size() > 0 && StringUtils.isNotEmpty(detail.getWpid())) {
                    zjRecords.stream()
                            .filter(zj -> StringUtils.isNotEmpty(zj.getWpid()))
                            .filter(zjRecord -> zjRecord.getWpid().equals(detail.getWpid()))
                            .findFirst().ifPresent(zjRecordinfo -> detail.setSatisfaction(zjRecordinfo.getSatisfaction()));
                }
            }

            if (StringUtils.isNotEmpty(detail.getDeptLevel2())) {
                if (depts.size() > 0) {
                    //主办单位名称赋值
                    depts.stream()
                            .filter(sysDept -> Long.valueOf(detail.getDeptLevel2()).equals(sysDept.getDeptId()))
                            .findAny().ifPresent(dept -> detail.setMainUnitName(dept.getDeptName()));
                }
                if (StringUtils.isNotEmpty(detail.getDeptLevel2vice1())){
                    //协办单位1名称赋值
                    depts.stream()
                            .filter(sysDept -> Long.valueOf(detail.getDeptLevel2vice1()).equals(sysDept.getDeptId()))
                            .findAny().ifPresent(dept -> detail.setAssistUnit1Name(dept.getDeptName()));
                }

                if (StringUtils.isNotEmpty(detail.getDeptLevel2vice2())){
                    //协办单位2名称赋值

                    depts.stream()
                            .filter(sysDept -> Long.valueOf(detail.getDeptLevel2vice2()).equals(sysDept.getDeptId()))
                            .findAny().ifPresent(dept -> detail.setAssistUnit2Name(dept.getDeptName()));
                }

                if (wpHandleLogs.size() > 0) {
                    wpHandleLogs.stream()
                            .filter(handlog -> detail.getWpid().equals(handlog.getWpid()))
                            .max(Comparator.comparing(WpHandleLog::getUpdateTime))
                            .ifPresent(handleLog -> {
                                //流转时间
                                detail.setEndStateTime(handleLog.getUpdateTime());
                            });
                }
                if (wpHandleLogsVisit.size() > 0) {
                    wpHandleLogsVisit.stream()
                            .filter(logVisit -> detail.getWpid().equals(logVisit.getWpid()))
                            .max(Comparator.comparing(WpHandleLog::getUpdateTime))
                            .ifPresent(handleLog -> {
                                //流转时间
                                detail.setCompletionDate(handleLog.getUpdateTime());
                            });
                }

                if (wpHandleLogsDept.size() > 0) {
                    wpHandleLogsDept.stream()
                            .filter(logDept -> detail.getWpid().equals(logDept.getWpid()))
                            .max(Comparator.comparing(WpHandleLog::getUpdateTime))
                            .ifPresent(handleLog -> {
                                //承办单位处理内容
                                detail.setMessage(handleLog.getMessage());
                                //是否解决
                                detail.setSolved(handleLog.getSolved());
                            });
                }
            }
        }
        return wpInfoDetailList;

    }

    protected TableDataInfo getDataTable(List<?> list, List<?> list1) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list1).getTotal());
        return rspData;
    }
}
